#include "../ExEngine/common/util.h"
#include "../ExEngine/engine/engine.h"
#include "../ExEngine/engine/object.h"
#include "../ExEngine/engine/camera.h"
#include "../ExEngine/render/d3d_mesh.h"
#include "../ExEngine/render/d3d.h"
#include "scene.h"

Particle SpawnFunctionA(MATRIX4X4F& offset)
{
	Particle particle; 
	EE_ZEROMEMORY(&particle, sizeof(Particle)); 
	particle.velocity[0] = ((float)((rand() % 10) - 5)) * 1.85f; 
	particle.velocity[1] = CUtil::random(5.0f, 10.0f) * 15;
	particle.velocity[2] = ((float)((rand() % 10) - 5)) * 1.88f; 
	particle.totallife = .0750f + CUtil::random(0.13f); 
	particle.radius = 2500.0f * CUtil::random(0.15f, 1.47f); 
	particle.color[0] = 1.0f; 
	particle.color[1] = CUtil::random(0.6f,1.0f);
	particle.color[2] = CUtil::random(0.2f,1.0f);
	particle.acceleration[1] = 0.0f; 
	return particle; 
}
Particle SpawnFunctionB(MATRIX4X4F& offset)
{
	float smokeStart = 13;
	Particle particle; 
	EE_ZEROMEMORY(&particle, sizeof(Particle)); 
	particle.velocity[0] = ((float)((rand() % 10) - 5)); // * 0.1f; 
	particle.velocity[1] = CUtil::random(5.0f, 10.0f) * 10;
	particle.velocity[2] = ((float)((rand() % 10) - 5)); // * 0.1f; 
	particle.totallife = .30f; 
	particle.radius = 4000.0f * CUtil::random(0.25f, 1.57f); 

	particle.color[0] = 
	particle.color[1] = 
	particle.color[2] = CUtil::random(0.15f, 1.0f);
	particle.acceleration[1] = 9.8f; 
	particle.position[1] = smokeStart;
	return particle; 
}
Particle SpawnFunctionC(MATRIX4X4F& offset)
{
	float smokeStart = 10;
	Particle particle; 
	EE_ZEROMEMORY(&particle, sizeof(Particle)); 
	particle.velocity[0] = ((float)((rand() % 10) - 5)) * 1.4f; 
	particle.velocity[1] = CUtil::random(5.0f, 10.0f) * 11;
	particle.velocity[2] = ((float)((rand() % 10) - 5)) * 1.4f; 
	particle.totallife = 0.0595f;  

	if(CUtil::random(0.05,1.0) > (0.1))
	{
		particle.totallife = 0.04595f * 2.5;  
		particle.radius = CUtil::random(0.05,1.0) * 9000;
	}
	else
	{
		
		particle.radius = CUtil::random(0.05,1.0) * 3000;
		particle.totallife = 0.04595f * 5;  
	}
	

	particle.color[0] = 1.0f; 
	particle.color[1] = 1.0f; 
	particle.color[2] = 1.0f; 
	particle.acceleration[1] =  0; 
	particle.position[1] = smokeStart + CUtil::random(2);
	particle.position[2] = CUtil::random(-1,1) * .82;
	particle.position[0] = CUtil::random(-1,1) * .82;
	return particle; 
}
CFire::CFire() : CObject()
{
	pLight = NULL; 
	pParticlesA = NULL;
	pParticlesB = NULL;
	pParticlesC = NULL;
	
}

HRESULT CFire::Create(void)
{
	Release();
	CD3D9Device *pRender = CEngine::GetInstance()->GetRenderer();
	pRender->CreateLight(&pLight);
	pLight->SetPosition(getPosition());
	pLight->SetDiffuseColor(COLOR3F(1,1,1));

	pRender->CreateParticleSystem(&pParticlesA, string("effects/fire_flame.dds"));
	pRender->CreateParticleSystem(&pParticlesB, string("effects/fire_smoke.dds"));
	pRender->CreateParticleSystem(&pParticlesC, string("effects/fire_ash.dds"));
	
	pParticlesA->setCount(50);
	pParticlesA->setSpawnFunction(SpawnFunctionA);
	pParticlesB->setCount(50);
	pParticlesB->setSpawnFunction(SpawnFunctionB);
	pParticlesC->setCount(50);
	pParticlesC->setSpawnFunction(SpawnFunctionC);

//	pFire->getModel()->setRenderFlags(RENDER_DEFAULT);
	return S_OK;
}

HRESULT CFire::Release(void)
{
	return S_OK;
}
int lastFlick =0;
int flickTime = 250;
float aux = 0;
void CFire::Update(const float dt)
{
#define FLICK_ANIM 20
	float time = CEngine::GetInstance()->GetTimeDuration() * 0.1 * flickTime;


	//2,6,4,3,6,4,3,2,6,3};

	int flickIdx = ((int)time) % 10;
	if(flickIdx != lastFlick)
	{/*
		int flickers[FLICK_ANIM] = {1};
		for(int i = 0; i < FLICK_ANIM; i++)
		flickers[i] = CUtil::random(1,7);*/
		flickTime = CUtil::random(250,350);
		lastFlick = flickIdx;
		aux = CUtil::random(0.0251f,0.06f);//int flickMul = flickers[flickIdx];
		pLight->SetAttenuation(aux * 3);
		
		if(CUtil::random(1) > 0)
			pLight->SetDiffuseColor(COLOR3F(1,0.75,0.22) );
		else
		{
			pLight->SetDiffuseColor(COLOR3F(0.65,0.47,0.22) );
		}
	}

	//aux += (10*dt);
	CEngine::GetInstance()->GetRenderer()->WriteLine(ScreenText::BOLD_FACE, "%f", aux);

	float x = 0; sinf(CEngine::GetInstance()->GetTimeDuration() * 5) * 20;
	float z = 0;cosf(CEngine::GetInstance()->GetTimeDuration() * 5) * 20;
	pLight->SetPosition(Vector3f(x,10,z));
	
	pLight->SetLightCoefficents(0,.75,1.5);

//(sinf(time) + 0.5) 
	//pLight->d
	
	pParticlesA->setCount(100);
	//pParticlesA->setTransform(MATRIX4X4F::translate(Vector3f(0,10,0)));

	pParticlesB->setEmitFrequency(5);
	pParticlesA->Update(dt);
	pParticlesB->Update(dt);
	pParticlesC->Update(dt);
	pParticlesC->setEmitFrequency(145);
	pParticlesC->setCount(200);
}

void CFire::setPosition(const Vector3f &position)
{
	MATRIX4X4F particleSys;
	pLight->SetPosition(position);
	
	particleSys = MATRIX4X4F::translate(position);
	pParticlesB->SetTransform(particleSys);
	pParticlesC->setTransform(particleSys);
	particleSys = MATRIX4X4F::translate(position + Vector3f(0,10,0));
	pParticlesA->setTransform(particleSys);
	
	m_transform.SetTranslation(position);
}

void CFire::Draw(void)
{

	pParticlesA->Draw();
	pParticlesB->Draw();
	pParticlesC->Draw();
}